package game.module.mj.processor;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.RoomServer;
import game.common.PlayingRoleMsgProcessor;
import game.entity.PlayingRole;
import game.module.mj.bean.MjRound;
import game.module.mj.logic.MjDataManager;
import game.module.mj.logic.MjLogic;
import game.session.SessionManager;
import lion.common.MsgCodeAnn;
import lion.netty4.message.MyRequestMessage;
import lion.netty4.message.RequestMessageRaw;
import lion.netty4.message.RequestProtoMessage;
import lua.LuaMessageLogin.S2ErrorCode;
import lua.LuaMessageMj.C2SMJNextRound;
import lua.LuaMessageMj.PushMJNextRound;
import lua.LuaMessageMj.PushMjNewGame;
import lua.LuaMessageMj.PushRoundOver;

@MsgCodeAnn(msgcode = C2SMJNextRound.id, accessLimit = 500)
public class MjNextRoundProcessor extends PlayingRoleMsgProcessor {

	private static Logger logger = LoggerFactory.getLogger(MjNextRoundProcessor.class);

	@Override
	public void process(PlayingRole playingRole, RequestMessageRaw requestMessage) throws Exception {

		int playerId = playingRole.getId();
		final MjRound mjround = MjDataManager.getInstance().getMjRoom(playerId);
		if (mjround == null) {
			S2ErrorCode retMsg = new S2ErrorCode(201);
			playingRole.writeAndFlush(retMsg.build(playingRole.alloc()));
			return;
		}
		int m_wCurrentUser = mjround.getPlayerIds().indexOf(playerId);
		boolean palyRead[] = mjround.getPlayReady();
		List<Integer> plays = mjround.getPlayerIds();
		
		PushMJNextRound pushmsg = new PushMJNextRound();
		pushmsg.player_index = (byte) plays.indexOf(playerId);
		for(Integer aPlayer: plays) {
			PlayingRole pr = SessionManager.getInstance().getPlayer(aPlayer);
			if (pr != null && pr.isChannelActive()) {
				pr.writeAndFlush(pushmsg.build(pr.alloc()));
			}
		}
		
		if (palyRead[m_wCurrentUser] == true) {
			return;// 该玩家已经准备好了
		}
		palyRead[m_wCurrentUser] = true;
		for (boolean flag : palyRead) {
			if (flag == false)
				return;
		}
		MjLogic.getInstance().nextRound(mjround);

		if (mjround.getRoundSum() > 0) {
			MjLogic.getInstance().randCard(mjround);
			byte[][] cards = mjround.getCards();
			PushMjNewGame pushMsg = new PushMjNewGame();
			for (Integer aPlayerId : plays) {
				PlayingRole pr = SessionManager.getInstance().getPlayer(aPlayerId);
				if (pr != null && pr.isChannelActive()) {
					pushMsg.cards = cards[plays.indexOf(aPlayerId)];
				//	logger.info("cardds[{}]={}", plays.indexOf(aPlayerId), pushMsg.cards);
					pr.writeAndFlush(pushMsg.build(pr.alloc()));
					// 推送关于出牌的信息
				}
			}
		}
		// 下一轮发牌
		if (mjround.getRoundSum() > 0) {
			RoomServer.executorService.schedule(new Runnable() {

				@Override
				public void run() {

					MjLogic.getInstance().GameStart(mjround);
				}
			}, 2, TimeUnit.SECONDS);
		} else {
			for (Integer aPlayerId : plays) {
				MjDataManager.getInstance().removePlayer(aPlayerId);
			}

			
			
			// mjround.set

		}

	}

	@Override
	public void processProto(PlayingRole playingRole, RequestProtoMessage request) throws Exception {

	}

	@Override
	public void processWebsocket(PlayingRole playingRole, MyRequestMessage request) throws Exception {

	}

}
